# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=assetnote-wordlists
pkgver=180.8a2765b
pkgrel=1
pkgdesc='Assetnote generated wordlists.'
groups=('blackarch-wordlist')
arch=('any')
url='https://github.com/assetnote/wordlists'
license=('Apache-2.0')
makedepends=('git')
source=("$pkgname::git+https://github.com/assetnote/wordlists.git")
sha512sums=('SKIP')

pkgver() {
  cd $pkgname

  ( set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null |
      sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "%s.%s" "$(git rev-list --count HEAD)" \
      "$(git rev-parse --short=7 HEAD)"
  )
}

package() {
  cd $pkgname

  datasets=("automated" "kiterunner" "manual" "technologies")

  for dataset in "${datasets[@]}"; do
    install -dm 755 "$pkgdir/usr/share/$pkgname/$dataset"

    curl -s https://wordlists-cdn.assetnote.io/data/$dataset/ |
    grep -oP 'href="\K[^"]+\.[a-zA-Z0-9]+' |
    awk -F'_' '{prefix=$1; for (i=2; i<=NF-3; i++) prefix=prefix"_"$i} !seen[prefix]++ {latest[prefix]=$0} {latest[prefix]=$0} END {for (p in latest) print latest[p]}' |
    xargs -I {} curl -o "$pkgdir/usr/share/$pkgname/$dataset/{}" \
    -L "https://wordlists-cdn.assetnote.io/data/$dataset/{}"
  done
}

